﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Data;
using Microsoft.Practices.Prism.Commands;
using ParaEditor.Common;
using ParaEditor.DAO;
using ParaEditor.DAO.ParaDAO;
using ParaEditor.Desktop.Helper;
using ParaEditor.Desktop.Interfaces;
using ParaEditor.Domain.Entities;
using ParaEditor.DomainSource.Entities;
using ParaEditor.Infrastructure;

namespace ParaEditor.Desktop.ViewModel
{
    [Export(typeof(IPara3006ViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class Para3006ViewModel : ViewModelBase , IPara3006ViewModel
    {
        private Para3006DAO p3006Dao;
        private Para3011DAO p3011Dao;
        private BenfInfoDAO benfDao;
        private SourceBaseDAO sBaseDAO;

        private Para3011 crtPara;
        public Para3011 CrtPara
        {
            get
            {
                return crtPara;
            }
            set
            {
                this.crtPara = value;
                this.RaisePropertyChangedEvent("CrtPara");
            }
        }

        private string stationNmCn;
        public string StationNmCn
        {
            get
            {
                return this.stationNmCn;
            }
            set
            {
                this.stationNmCn = value;
                this.RaisePropertyChangedEvent("StationNmCn");
            }
        }

        private string lineID;
        public string LineID
        {
            get
            {
                return lineID;
            }
            set
            {
                this.lineID = value;
                this.RaisePropertyChangedEvent("LineID");
            }
        }

        private string stationNID;
        public string StationNID
        {
            get
            {
                return stationNID;
            }
            set
            {
                this.stationNID = value;
                this.RaisePropertyChangedEvent("StationNID");
            }
        }

        private string stationNameCN;
        public string StationNameCN
        {
            get
            {
                return stationNameCN;
            }
            set
            {
                this.stationNameCN = value;
                this.RaisePropertyChangedEvent("StationNameCN");
            }
        }

        private string stationNameEN;
        public string StationNameEN
        {
            get
            {
                return stationNameEN;
            }
            set
            {
                this.stationNameEN = value;
                this.RaisePropertyChangedEvent("StationNameEN");
            }
        }

        private ICollectionView infoVerList;
        public ICollectionView InfoVerList
        {
            get
            {
                return infoVerList;
            }
            set
            {
                this.infoVerList = value;
                this.RaisePropertyChangedEvent("InfoVerList");
            }
        }

        private ICollectionView paraCollectionDetail;
        public ICollectionView ParaCollectionDetail
        {
            get
            {
                return this.paraCollectionDetail;
            }
            set
            {
                this.paraCollectionDetail = value;
                this.RaisePropertyChangedEvent("ParaCollectionDetail");
            }
        }

        private ICollectionView lineList;
        public ICollectionView LineList
        {
            get
            {
                return this.lineList;
            }
            set
            {
                this.lineList = value;
                this.RaisePropertyChangedEvent("LineList");
            }
        }

        private ICollectionView resList;
        public ICollectionView ResList
        {
            get
            {
                return this.resList;
            }
            set
            {
                this.resList = value;
                this.RaisePropertyChangedEvent("ResList");
            }
        }

        private Visibility editGroupBoxVisible;
        public Visibility EditGroupBoxVisible
        {
            get
            {
                return this.editGroupBoxVisible;
            }
            set
            {
                this.editGroupBoxVisible = value;
                this.RaisePropertyChangedEvent("EditGroupBoxVisible");
            }
        }

        //Button
        public DelegateCommand AddParamCommand { get; private set; }
        public DelegateCommand DelParamCommand { get; private set; }
        public DelegateCommand SaveParamCommand { get; private set; }
        public DelegateCommand CanclEditParamCommand { get; private set; }
        public DelegateCommand NewVersionCommand { get; private set; }
        public DelegateCommand CopyCurrentVersionCommand { get; private set; }
        public DelegateCommand ImportBenfInfoCommand { get; private set; }
        public DelegateCommand ImportStationInfoCommand { get; private set; }

        private ICollectionView benfList;
        public ICollectionView BenfList
        {
            get
            {
                return this.benfList;
            }
            set
            {
                this.benfList = value;
                this.RaisePropertyChangedEvent("BenfList");
            }
        }

        [ImportingConstructor]
        public Para3006ViewModel()
        {
            sBaseDAO = new SourceBaseDAO();
            CrtPara = new Para3011();
            CrtPara.movDetail = "测试";
            this.StationNmCn = "测试";
            p3006Dao = new Para3006DAO();
            p3011Dao = new Para3011DAO();
            benfDao = new BenfInfoDAO();
            InfoVerList = CollectionViewSource.GetDefaultView(p3006Dao.GetAllVersion());
            if (p3006Dao.GetAllVersion().Count == 0)
            {
                Microsoft.Windows.Controls.MessageBox.Show("当前参数无任何参数版本");
            }
            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
            EditGroupBoxVisible = Visibility.Hidden;

            ResList = CollectionViewSource.GetDefaultView(p3011Dao.GetObjectByMaxInfoVer());

            BenfList = CollectionViewSource.GetDefaultView(benfDao.GetCurrentBenfInfo());

            LineList = CollectionViewSource.GetDefaultView(p3006Dao.GetCurrentLine());
            //Button
            this.AddParamCommand = new DelegateCommand(this.AddParam);
            this.DelParamCommand = new DelegateCommand(this.DelParam);
            this.SaveParamCommand = new DelegateCommand(this.SaveParam);
            this.CanclEditParamCommand = new DelegateCommand(this.CanclEditParam);
            this.NewVersionCommand = new DelegateCommand(this.NewVersion);
            this.CopyCurrentVersionCommand = new DelegateCommand(this.CopyCurrentVersion);
            this.ImportBenfInfoCommand = new DelegateCommand(this.ImportBenfInfo);
            this.ImportStationInfoCommand = new DelegateCommand(this.ImportStationInfo);
        }

        private void ImportStationInfo()
        {
            int maxID = p3006Dao.GetMaxID();
            //从secu查询受益方信息
            IList<CrStationInfo> ownerList = sBaseDAO.GetCurrentStationInfo();
            if (ownerList.Count == 0)
            {
                Microsoft.Windows.Controls.MessageBox.Show("未查询到数据，不进行导入！");
                return;
            }
            //转换
            IList<Para3006> stationInfos = DomainConvert.ConvertToPara3006(ownerList, maxID + 1);
            ResultValue resultValue = p3006Dao.BatchCreateObject(stationInfos);
            if (resultValue.Code == 0)
            {
                Microsoft.Windows.Controls.MessageBox.Show("导入车站信息成功");
                ParaCollectionDetail = CollectionViewSource.GetDefaultView(p3006Dao.GetObjectByInfoVer(maxID + 1));
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("保存失败！发生错误:" + resultValue.Msg);
            }
        }

        private void ImportBenfInfo()
        {
            int maxID = p3006Dao.GetMaxBenfVer();
            //从secu查询受益方信息
            IList<OwnerInfo> ownerList = sBaseDAO.GetCurrentOwnerInfo();
            //转换
            IList<BenfInfo> benfInfos = DomainConvert.ConvertToBenfInfo(ownerList, maxID + 1);
            ResultValue resultValue = benfDao.BatchCreateObject(benfInfos);
            if (resultValue.Code == 0)
            {
                Microsoft.Windows.Controls.MessageBox.Show("生成受益方成功");
                BenfList = CollectionViewSource.GetDefaultView(benfDao.GetCurrentBenfInfo());
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("保存失败！发生错误:" + resultValue.Msg);
            }
        }
        
        void InfoVerList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (InfoVerList.CurrentItem != null)
            {
                ReflashDataGrid();
            }
        }

        private void AddParam()
        {
            if (InfoVerList.CurrentItem == null)
            {
                Microsoft.Windows.Controls.MessageBox.Show("请添加参数版本！才可进行添加");
            }
            else
            {
                EditGroupBoxVisible = Visibility.Visible;
            }
            
        }

        private void DelParam()
        {
            try
            {
                if (ParaCollectionDetail.CurrentItem != null)
                {
                    Para3006 param = ParaCollectionDetail.CurrentItem as Para3006;
                    if (Microsoft.Windows.Controls.MessageBox.Show("确认删除所选择的参数！", "3006参数", System.Windows.MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.Question) == System.Windows.MessageBoxResult.OK)
                    {
                        //删除
                        ResultValue result = p3006Dao.DeleteObject(param, param.para3006Pk.StationVer);
                        if (result.Code == 0)
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("删除成功");
                            //刷新
                            InfoVerList = CollectionViewSource.GetDefaultView(p3006Dao.GetAllVersion());
                            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;

                            ReflashDataGrid();
                            
                        }
                        else
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("删除失败!发生错误：" + result.Msg);
                        }
                    }
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("未选择要删除的参数");
                }
            }
            catch(Exception e)
            { 
            }
        }

        public void ReflashDataGrid()
        {
            int infoVer = (int)InfoVerList.CurrentItem;
            ParaCollectionDetail = CollectionViewSource.GetDefaultView(p3006Dao.GetObjectByInfoVer(infoVer));
        }

        public void SaveParam()
        {
            if (Confirm())
            {
                BenfInfo benfInfo = BenfList.CurrentItem as BenfInfo;
                int point = 0;
                Para3006 p3006 = new Para3006();
                Para3006Pk p3006Pk = new Para3006Pk();
                p3006Pk.StationVer = (int)InfoVerList.CurrentItem;

                p3006Pk.StationNid = this.StationNID;
                p3006.para3006Pk = p3006Pk;
                point = p3006Dao.GetPointByCnName(this.StationNameCN);
                
                if (point > 0)
                {
                    p3006.StationNmCn = this.StationNameCN;
                    p3006.StationNmCnPoint = point;
                    p3006.StationNmEn = this.StationNameEN;
                    p3006.LineId = (this.LineList.CurrentItem as CurrentLine).LineID;
                    p3006.StaId = Int32.Parse(this.StationNID.Substring(2, 2));
                    p3006.OwnerId = benfInfo.benfInfoPk.benfId;
                    p3006.OwnerId = p3006Dao.GetBenfID(p3006.LineId).PadLeft(4,'0');
                    ResultValue result = p3006Dao.CreateObject(p3006);
                    if (result.Code == 0)
                    {
                        Microsoft.Windows.Controls.MessageBox.Show("保存成功");
                        ReflashDataGrid();

                        CanclEditParam();
                    }
                    else
                    {
                        Microsoft.Windows.Controls.MessageBox.Show("保存失败！发生错误:" + result.Msg);
                    }
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("保存失败！未找到此车站名相应的指针，检查此车站名是否正确，或是否存在于3011中!");
                }
            }

        }

        private void CanclEditParam()
        {
            this.EditGroupBoxVisible = Visibility.Hidden;
            this.LineID = null;
            this.StationNID = null;
            this.StationNameCN = null;
            this.StationNameEN = null;
        }

        private bool Confirm()
        {
            bool flag = true;
            if (this.StationNID != null && !"".Equals(this.StationNID) && this.LineList.CurrentItem != null && this.StationNameCN != null && !"".Equals(this.StationNameCN) && !"".Equals(this.StationNameEN) && this.StationNameEN != null && this.BenfList.CurrentItem != null)
            {
                flag = true;
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("所填信息不完整");
                flag = false;
            }
            return flag;
        }

        private void NewVersion()
        {
            ObservableCollection<int> infoVerCollection = new ObservableCollection<int>();
            infoVerCollection = p3006Dao.GetAllVersion();
            int newVersion = p3006Dao.GetMaxID() + 1;
            infoVerCollection.Add(newVersion);
            InfoVerList = CollectionViewSource.GetDefaultView(infoVerCollection);
            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
        }

        private void CopyCurrentVersion()
        {
            //查询当前版本
            ObservableCollection<Para3006> currentVersionParam = new ObservableCollection<Para3006>();
            int infoVer = (int)InfoVerList.CurrentItem;
            currentVersionParam = p3006Dao.GetObjectByInfoVer(infoVer);

            if (currentVersionParam.Count != 0)
            {
                Para3006Pk p3006Pk;
                Para3006 new3006;
                int newVersion = p3006Dao.GetMaxID() + 1;
                foreach (Para3006 item in currentVersionParam)
                {

                    new3006 = new Para3006();
                    p3006Pk = new Para3006Pk();
                    
                   
                    p3006Pk.StationVer = newVersion;
                    p3006Pk.StationNid = item.para3006Pk.StationNid;
                    
                   new3006.para3006Pk = p3006Pk;
                    
                   new3006.StationNmCn = item.StationNmCn;
                   new3006.StationNmEn = item.StationNmEn;
                   new3006.LineId = item.LineId;
                   new3006.LineIdbcd = item.LineIdbcd;
                   new3006.StationIdbcd = item.StationIdbcd;
                   new3006.StationNmCnPoint = item.StationNmCnPoint;
                   new3006.OwnerId = item.OwnerId;
                   new3006.StaId = item.StaId;
                   

                    /*
                    item.para3006Pk = null;
                    new3006 = item;
                    new3006.para3006Pk = p3006Pk;
                    */
                    ResultValue result = p3006Dao.CreateObject(new3006);
                    if (result.Code != 0)
                    {
                        Microsoft.Windows.Controls.MessageBox.Show("复制过程中出现错误!错误信息：" + result.Msg);
                    }
                }
                //刷新
                ParaCollectionDetail = CollectionViewSource.GetDefaultView(p3006Dao.GetObjectByInfoVer(newVersion));
                InfoVerList = CollectionViewSource.GetDefaultView(p3006Dao.GetAllVersion());
                InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("所查询的当前版本没有数据,不可进行复制");
            }
        }
    }
}
